<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>选项卡</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }

      ul,
      ol,
      li {
        list-style: none;
      }

      .box {
        width: 600px;
        height: 400px;
        border: 3px solid pink;
        margin: 50px auto;
        display: flex;
        flex-direction: column;
      }

      .box > ul {
        height: 60px;
        display: flex;
      }

      .box > ul > li {
        flex: 1;
        background-color: skyblue;
        color: #fff;
        font-size: 20px;
        display: flex;
        justify-content: center;
        align-items: center;
        cursor: pointer;
      }

      .box > ul > li.active {
        background-color: orange;
      }

      .box > ol {
        flex: 1;
        position: relative;
      }

      .box > ol > li {
        width: 100%;
        height: 100%;
        background-color: purple;
        position: absolute;
        left: 0;
        top: 0;
        font-size: 100px;

        display: none;
      }

      .box > ol > li.active {
        display: block;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <ul>
        <li class="active">a</li>
        <li>b</li>
        <li>c</li>
        <li>d</li>
      </ul>
      <ol>
        <li class="active">1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
      </ol>
    </div>
    <script>
      var ul = document.querySelector('.box ul');
      var lis = document.querySelectorAll('.box ul li');
      var ol = document.querySelector('.box ol');
      var lis1 = document.querySelectorAll('.box ol li');
      ul.addEventListener('click', fn);
      function fn(e) {
        lis.forEach(function (ele) {
          ele.classList.remove('active');
        });
        console.dir(e.target);
        if (e.target.tagName === 'LI') {
          //当点击a的时候显示为li的标签名切换为active ,1显示出来,点击b的时候标签名切换为active ,2显示出来
          lis1.forEach(function (item, index) {
            //将伪数组转为真数组
            var arr = [...lis];
            //获取下标里面有没有'active'标签
            if (index === arr.indexOf(e.target)) {
              item.classList.add('active');
            } else {
              item.classList.remove('active');
            }
          });
          e.target.classList.add('active');
        }
      }
    </script>
  </body>
</html>
